package com.cwvs.utils;

import java.io.Serializable;
import java.util.List;

import org.apache.commons.lang.StringUtils;

public class Pages implements Serializable{

    /**
     * 
     */
    private static final long serialVersionUID = 4132632652870352925L;
    private int totalRecord; // 总记录数
    private int totalPage; // 总页数
    private int currentRecord; // 当前页记录数
    private int currentPage = 1; // 当前页数

    private int pageSize = 10; // 每页大小
    
    private int beginRow = 0;
    private int endRow;

    private String beginDate; // 开始时间
    private String endDate; // 结束时间

    private int prevPage;
    private int nextPage;

    private boolean hasPrevPage;
    private boolean hanNextPage;
    
    private int       pageCount = 0; // 总页数
    private String listPageHtml = ""; //前台列表分页的HTML代码
    
    private String    parameters  = "";

    public int getPageCount() {
        return pageCount;
    }

    public String getParameters() {
        return parameters;
    }

    public void setParameters(String parameters) {
        this.parameters = parameters;
    }

    public void setPageCount(int pageCount) {
        this.pageCount = pageCount;
    }

    public String getListPageHtml() {
        return listPageHtml;
    }

    public void setListPageHtml(String listPageHtml) {
        this.listPageHtml = listPageHtml;
    }

    public int getPrevPage() {
        return prevPage;
    }

    public void setPrevPage(int prevPage) {
        this.prevPage = prevPage;
    }

    public int getNextPage() {
        return nextPage;
    }

    public void setNextPage(int nextPage) {
        this.nextPage = nextPage;
    }

    public boolean isHasPrevPage() {
        return hasPrevPage;
    }

    public void setHasPrevPage(boolean hasPrevPage) {
        this.hasPrevPage = hasPrevPage;
    }

    public boolean isHanNextPage() {
        return hanNextPage;
    }

    public void setHanNextPage(boolean hanNextPage) {
        this.hanNextPage = hanNextPage;
    }

    private List<? extends Object> page;

    public List<? extends Object> getPage() {
        return page;
    }

    public void setPage(List<? extends Object> page) {
        this.page = page;
    }

    public int getTotalRecord() {
        return totalRecord;
    }

    public void setTotalRecord(int totalRecord) {
        this.totalRecord = totalRecord;
    }

    public int getTotalPage() {
        return totalPage;
    }

    public void setTotalPage(int totalPage) {
        this.totalPage = totalPage;
    }

    public int getCurrentRecord() {
        return currentRecord;
    }

    public void setCurrentRecord(int currentRecord) {
        this.currentRecord = currentRecord;
    }

    public int getCurrentPage() {
        return currentPage;
    }

    public void setCurrentPage(int currentPage) {
        this.currentPage = currentPage;
    }

    // public void setCurrentPage(String currentPage) {
    // if(!StringUtils.isNumeric(currentPage)){
    // this.currentRecord = 1;
    // }
    // this.currentRecord = Integer.parseInt(currentPage);
    // }

    public int getBeginRow() {
        return beginRow;
    }

    public void setBeginRow(int beginRow) {
        this.beginRow = beginRow;
    }

    public int getEndRow() {
        return endRow;
    }

    public void setEndRow(int endRow) {
        this.endRow = endRow;
    }

    public String getBeginDate() {
        return beginDate;
    }

    public void setBeginDate(String beginDate) {
        this.beginDate = beginDate;
    }

    public String getEndDate() {
        return endDate;
    }

    public void setEndDate(String endDate) {
        this.endDate = endDate;
    }

    public int getPageSize() {
        return pageSize;
    }
    
    public void setPageSize(int pageSize) {
        this.pageSize = pageSize;
    }
    
    public void setRowCount(int rowCount) {
        this.totalRecord = rowCount;
        pageCount = rowCount % pageSize == 0 ? rowCount / pageSize : (rowCount/ pageSize + 1);
        if (currentPage < 1) {
            this.currentPage = 1;
        } else if (currentPage > pageCount && pageCount == 0) {
            this.currentPage = 1;
        } else if (currentPage > pageCount && pageCount != 0) {
            this.currentPage = pageCount;
        } 
        StringBuffer pageHtml = new StringBuffer();
        pageHtml.append("<script language=\"javascript\" type=\"text/javascript\"> \n");
        pageHtml.append("    function QueryString(item){");
        pageHtml.append("        var sValue=location.search.match(new RegExp(\"[\\?\\&]\"+item+\"=([^\\&]*)(\\&?)\",\"i\")); \n");
        pageHtml.append("        alert(sValue);return sValue?sValue[1]:sValue; \n");
        pageHtml.append("    } \n");
        //不允许超过一百页
        //if(this.pageCount > 100) {
        //    pageHtml.append("    var count = "+ this.pageSize*100 +";  \n");
        //} else {
            pageHtml.append("    var count = "+ this.totalRecord +";  \n");
        //}
        pageHtml.append("    var perpage = "+ this.pageSize +";  \n");
        pageHtml.append("    var currentpage = "+ this.currentPage +"; \n ");
        pageHtml.append("    if (currentpage == null) { \n");
        pageHtml.append("        currentpage = 1; \n");
        pageHtml.append("    }else{ \n");
        pageHtml.append("        currentpage = parseInt(currentpage); \n");
        pageHtml.append("    } \n");
        pageHtml.append("    var pagecount = Math.ceil(count/perpage); \n");
        pageHtml.append("    var pagestr = \"\"; \n");
        pageHtml.append("    var breakpage = 5; \n ");
        pageHtml.append("    var currentPrePosition = 2;  \n");
        pageHtml.append("    var currentNextPosition = 4; \n");
        pageHtml.append("    var maxspace = 4; \n");
        pageHtml.append("    var prevnum = currentpage - currentPrePosition;  \n ");
        pageHtml.append("    var nextnum = currentpage + currentNextPosition; \n ");
        pageHtml.append("    if(prevnum < 1) { \n");
        pageHtml.append("        prevnum = 1; \n");
        pageHtml.append("    }\n");
        pageHtml.append("    if(nextnum > pagecount) { \n");
        pageHtml.append("        nextnum = pagecount; \n");
        pageHtml.append("    } \n");
        pageHtml.append("    pagestr += (currentpage==1)?'':'<a href=\"javascript:changePage('+(currentpage-1)+')\">上一页</a>'; \n");
        pageHtml.append("    if(prevnum > maxspace) { \n");
        pageHtml.append("        for(i = 1; i <= 2; i++) { \n");
        pageHtml.append("            if(i == 1){ \n");
        pageHtml.append("                pagestr += '<a href=\"javascript:changePage('+i+')\" >'+i+'</a>'; \n");
        pageHtml.append("            } else { \n");
        pageHtml.append("                pagestr += '<a href=\"javascript:changePage('+i+')\">'+i+'</a>'; \n");
        pageHtml.append("            } \n");
        pageHtml.append("         } \n");
        pageHtml.append("         pagestr += '<span>...</span>'; \n");
        pageHtml.append("    }else{ \n");
        pageHtml.append("        for(i = 1; i < prevnum; i++) { \n");
        pageHtml.append("            pagestr += '<a  href=\"javascript:changePage('+i+')\" >'+i+'</a>'; \n");
        pageHtml.append("        } \n");
        pageHtml.append("    } \n");
        pageHtml.append("    for(i = prevnum; i <= nextnum; i++) { \n");
        pageHtml.append("        if(i == currentpage) { \n");
        pageHtml.append("            pagestr += '<a class=\"cur\">'+i+'</a>'; \n");
        pageHtml.append("        } else { \n");
        pageHtml.append("            pagestr += '<a href=\"javascript:changePage('+i+')\" >'+i+'</a>'; \n");
        pageHtml.append("        } \n");
        pageHtml.append("    } \n");
        pageHtml.append("    if(pagecount - nextnum > 0) { \n");
        pageHtml.append("        for(i = nextnum + 1; i <= breakpage; i++) { \n");
        pageHtml.append("            pagestr += '<a href=\"javascript:changePage('+i+')\">'+i+'</a>'; \n"); 
        pageHtml.append("        } \n");
        pageHtml.append("        pagestr += '<span>...</span><a href=\"javascript:changePage('+pagecount+')\">'+pagecount+'</a>'; \n");
        pageHtml.append("        for(i = pagecount + 1; i <= pagecount; i++) { \n");
        pageHtml.append("            pagestr += '<a href=\"javascript:changePage('+i+')\">'+i+'</a>'; \n");
        pageHtml.append("        } \n");
        pageHtml.append("    }else{ \n");
        pageHtml.append("        for(i = nextnum + 1; i <= pagecount; i++) { \n");
        pageHtml.append("            pagestr += '<a href=\"javascript:changePage('+i+')\">'+i+'</a>'; \n");
        pageHtml.append("        } \n");
        pageHtml.append("    } \n");
        pageHtml.append("    if(pagecount > 0) { \n");
        pageHtml.append("       pagestr += (currentpage==pagecount)?'':'<a href=\"javascript:changePage('+(currentpage+1)+')\">下一页</a>'; \n");
        pageHtml.append("    } \n");
        pageHtml.append("    $('#pagebar').html(pagestr); \n");
        pageHtml.append("</script> \n");
        listPageHtml = pageHtml.toString();
    }

}
